<template>
  <div class="app-container">
    <el-tree
      :data="category"
      :props="defaultProps"
      show-checkbox
      node-key="catId"
      :expand-on-click-node="false"
      :default-expanded-keys="expanded"
      @node-click="handleCategoryNodeClick"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            type="text"
            size="mini"
            v-if="node.level <= 2"
            @click="() => append(data)"
          >
            增加
          </el-button>
          <el-button
            type="text"
            size="mini"
            v-if="node.childNodes.length == 0"
            @click="() => remove(node, data)"
          >
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
      <el-form :model="menu">
        <el-form-item label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="menu.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位" :label-width="formLabelWidth">
          <el-select v-model="menu.productUnit" placeholder="请选择计量单位">
            <el-option label="件" value="件"></el-option>
            <el-option label="辆" value="辆"></el-option>
            <el-option label="个" value="个"></el-option>
            <el-option label="瓶" value="瓶"></el-option>
            <el-option label="张" value="张"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMenu">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { tree, delMenus, addMenus } from "@/api/product/product.js";
export default {
  data() {
    return {
      menu: {
        name: "",
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        productUnit: "",
      },
      dialogFormVisible: false,
      formLabelWidth: "120px",
      expanded: [], // 默认展开的节点，防止删除节点后树收缩了
      category: [],
      defaultProps: {
        children: "childrens",
        label: "name",
      },
    };
  },
  created() {
    this.getProductCategoryTree();
  },
  methods: {
    handleCategoryNodeClick(data) {
      // console.log(data);
    },
    getProductCategoryTree() {
      tree()
        .then((res) => {
          this.category = res.data;
        })
        .catch((err) => console.log(err));
    },
    append(data) {
      console.log(data);
      this.menu.parentCid = data.catId;
      this.menu.catLevel = data.catLevel * 1 + 1;
      this.dialogFormVisible = true;
    },

    addMenu() {
      addMenus(this.menu)
        .then((res) => {
          if (res.code == 0) {
            this.getProductCategoryTree();
            this.expanded = [this.menu.parentCid];
            this.menu = {
              name: "",
              parentCid: 0,
              catLevel: 0,
              showStatus: 1,
              sort: 0,
              productUnit: "",
            };
            this.dialogFormVisible = false;
          }
        })
        .catch((err) => {});
    },

    remove(node, data) {
      const menusIds = [node.data.catId];
      this.$confirm(`是否删除【${node.data.name}】菜单`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delMenus(menusIds)
            .then((res) => {
              if (res.code == 0) {
                this.$message({
                  type: "success",
                  message: "删除成功",
                });
                // 重新获取分类树
                this.getProductCategoryTree();
                this.expanded = [node.parent.data.catId];
              }
            })
            .catch((err) => {});
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
</style>
